﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace L9SubSet.lwf
{
	// 控制器挂载的灯具信息
	public class SubTubeData
	{
		public Rectangle MaxRect;				// 最大显示区域
		
		public List<PortTubeData> PortData;		// 端口灯具数据

		// 构造函数
		public SubTubeData()
		{
			MaxRect.X = 0;
			MaxRect.Y = 0;
			
			MaxRect.Width = 0;
			MaxRect.Height = 0;
			
			PortData = new List<PortTubeData>(); // 端口灯条的数据
		}

		// 清除灯具信息
		public int Clear()
		{
			for (int i = 0; i < PortData.Count(); i++)
			{
				PortData[i].Clear();
			}

			MaxRect.X = 0;
			MaxRect.Y = 0;
			
			MaxRect.Width = 0;
			MaxRect.Height = 0;

			return 0;
		}

		// 复制灯具信息
		public SubTubeData Copy()
		{
			SubTubeData sss = new SubTubeData();
			
			sss.MaxRect = new Rectangle(MaxRect.Left, MaxRect.Top, MaxRect.Right, MaxRect.Bottom);
			for (int i = 0; i < PortData.Count(); i++)
			{
				PortTubeData ttt = PortData[i].Copy();
				sss.PortData.Add(ttt);
			}

			return sss;
		}

		// 获得端口数量
		public int PortMax()
		{
			return PortData.Count;
		}

		// 添加端口
		public int AddPort()
		{
			PortData.Add(new PortTubeData());
			return 0;
		}

		// 添加端口
		public int AddPort(PortTubeData ppp)
		{
			PortData.Add(ppp);

			if (PortMax() == 1)
			{
				MaxRect = ppp.MaxRect;
			}
			else
			{
				if (ppp.MaxRect.Right > MaxRect.Right)
				{
					MaxRect.Width = MaxRect.Width + ppp.MaxRect.Right - MaxRect.Right;
				}

				if (ppp.MaxRect.X < MaxRect.X)
				{
					MaxRect.Width = MaxRect.Right - ppp.MaxRect.X;
					MaxRect.X = ppp.MaxRect.X;
				}

				if (ppp.MaxRect.Bottom > MaxRect.Bottom)
				{
					MaxRect.Height = MaxRect.Height + ppp.MaxRect.Bottom - MaxRect.Bottom;
				}

				if (ppp.MaxRect.Y < MaxRect.Y)
				{
					MaxRect.Height = MaxRect.Bottom - ppp.MaxRect.Y;
					MaxRect.Y = ppp.MaxRect.Y;
				}
			}

			return 0;
		}

		// 选择灯具
		public int SeleteTubeAt(int port, int index, int select)
		{
			PortData[port].SeleteTubeAt(index, select);
			return 0;
		}

		// 清除选择的灯具
		public int SelectClear()
		{
			for (int i = 0; i < PortData.Count(); i++)
			{
				PortData[i].SelectClear();
			}
			return 0;
		}

		// 比较控制器灯具数据
		public Boolean ComTubeData(SubTubeData tubedata)
		{
			// 判断对应主控的端口数是否有变化
			if (PortData.Count != tubedata.PortData.Count)
			{
				return true;
			}
			
			// 比较每个端口信息
			for (int i = 0; i < PortData.Count; i++)
			{
				Boolean result = PortData[i].ComPortData(tubedata.PortData[i]);
				if (result)
				{
					return true;
				}
			}
			
			return false;
		}
	}
}


